
function getMinutes(myfloat){
	minutes = myfloat.split(".")[1];
	if(!minutes){
		return 0;
	}
	if(minutes.length < 2){
		minutes = minutes + "0";
	}
	if(minutes.length > 2){
		minuts = minutes.substring(2);
	}
	minutes = 60 * parseFloat("0." + minutes);
	return minutes;
}

function getHours(myfloat){
	if(myfloat.indexOf(".") == -1){
		return parseInt(myfloat); 
	} else {
		return parseInt(myfloat.split("."));
	}
}

function getId(field){
	return parseInt(field.id.match(/[\d]/));
}


function getStartTimeId(id){
	return "#id_task_set-" + id + "-start_time";
}


function log(text){
	/*if(console){
		console.log(text);
	}*/
}


function getStartTime(id, actualWorkHours, actualWorkMinutes){	
	value = $(id).val();
	base = new Date("July 14, 2010 " + value);
	// add actualWorkHours
	base.setHours(base.getHours() + actualWorkHours);
	base.setMinutes(base.getMinutes() + actualWorkMinutes);
	log(base);
	return base.getHours()+":"+base.getMinutes()+":"+base.getSeconds();	
}



function setNext(id, startTimeId, nextStartTimeId){
	actual = $(id).val();
	actualWorkHours = getHours(actual);
	actualWorkMinutes = getMinutes(actual);
	startTime = getStartTime(startTimeId, actualWorkHours, actualWorkMinutes);
	$(nextStartTimeId).val(startTime);
}

function addNextShortcut(id, startTimeId, nextStartTimeId){
	shortStartTimeId = nextSartTimeId + " ~ " + "span.datetimeshortcuts"; 
	ahref = "&nbsp;<a  id=\"" + nextSartTimeId + "NextTime\" href=\"javascript:setNext('" + id + "', '" + startTimeId + "','" + nextStartTimeId + "');\">Next</a>";
	$(shortStartTimeId).append(ahref);
	log($(startTimeId));
}



function addShortcuts(){
	$("td.actual_work_hours > input").each(
		function(){			
			id = getId(this);
			startTimeId = getStartTimeId(id);
			nextSartTimeId = getStartTimeId(id+1);				
			addNextShortcut("#" + this.id, startTimeId, nextSartTimeId);
		}
	);	
}


$(document).ready(function() {
	var t=setTimeout("addShortcuts()", 2000);
});
